﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Manager.FormAdd_Edit;
using Manager.App_Code;
using System.Linq;
using System.Data.Linq;
using Manager.Common;

namespace Manager.FormMain
{
    public partial class Frm_BarButton_NhomKhach : DevExpress.XtraEditors.XtraForm
    {
        NewPearl_HotelDataContext db = new NewPearl_HotelDataContext();

        public Frm_BarButton_NhomKhach()
        {
            InitializeComponent();
        }

        private void Frm_BarButton_NhomKhach_Load(object sender, EventArgs e)
        {
            db.Refresh(RefreshMode.OverwriteCurrentValues, db.NhomKhaches);
            gridControl1.DataSource = db.NhomKhaches.ToList();
        }

        private void butThemMoi_Click(object sender, EventArgs e)
        {
            try
            {
                frm_GroupCustomer frmNhomKhach = new frm_GroupCustomer();
                frmNhomKhach.ShowDialog();
                Frm_BarButton_NhomKhach_Load(sender, e);
            }
            catch (Exception ex)
            {
                Log.WriteLog("Frm_BarButton_NhomKhach:butThemMoi_Click" + ex.Message + "===>" + ex.StackTrace);
            }
        }

        private void butXoa_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dr = MessageBox.Show("Bạn có muốn xóa không?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.OK)
                {
                    int index = gridView1.FocusedRowHandle;
                    var manhomkhach = (long)gridView1.GetRowCellValue(index, MaNhomKhach);
                    var nhomKhach = db.NhomKhaches.SingleOrDefault(i => i.MaNhomKhach == manhomkhach);
                    if (nhomKhach != null)
                    {
                        db.NhomKhaches.DeleteOnSubmit(nhomKhach);
                        db.SubmitChanges();
                        MessageBox.Show("Xóa dữ liệu thành công!");
                        this.Frm_BarButton_NhomKhach_Load(sender, e);
                    }
                    else
                        MessageBox.Show("Xóa dữ liệu thất bại!");
                }
            }
            catch (Exception ex)
            {
                Log.WriteLog("Frm_BarButton_NhomKhach:butXoa_Click" + ex.Message + "===>" + ex.StackTrace);
            }
        }

        private void butHieuChinh_Click(object sender, EventArgs e)
        {            
            int index = gridView1.FocusedRowHandle;
            var manhomkhach = (long)gridView1.GetRowCellValue(index, MaNhomKhach);
            var nhomKhach = db.NhomKhaches.SingleOrDefault(i => i.MaNhomKhach == manhomkhach);
            frm_GroupCustomer frmNhomKhach = new frm_GroupCustomer(nhomKhach);
            frmNhomKhach.ShowDialog();
            Frm_BarButton_NhomKhach_Load(null, null);
        }

        private void Frm_BarButton_NhomKhach_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F3)
                this.butThemMoi_Click(sender, e);
            if (e.KeyCode == Keys.F5)
                this.butHieuChinh_Click(sender, e);
            if (e.KeyCode == Keys.F7)
                this.butXoa_Click(sender, e);
        }
    }
}